package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.CookieUtils;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.pages.frontend.MainPage;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

/**
 * Created by chenmeng on 2016/5/26.
 */
public class CookieLoginTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(CookieLoginTest.class);

    private WebDriver driver;
    private KeyWordAction action;

    @Parameters({"nodeUrl","browser"})
    @BeforeClass(alwaysRun = true)
    public void setUp(String nodeUrl,String browser) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        super.init(driver);
        action = new KeyWordAction(driver);
    }


    @Parameters({"browser"})
    @AfterClass(alwaysRun = true)
    public void tearDown(String browser) {
        try{
            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }

    @Parameters({"userName","password"})
    @Test(description = "登录系统")
    public void LOGIN_FRONT(String userName,String password) throws Exception{
        action.LoginFront(userName,password);
    }

    @Test(description = "登出系统")
    public void LOGOUT_FRONT() throws Exception{
        action.LogoutFront();
    }

    @Parameters({"userName","password"})
    @Test(description = "登录系统")
    public void LOGIN_FRONT_NEW(String userName,String password) throws Exception{
        MainPage mainPage = new MainPage(driver,action);
        mainPage.openPage();
        CookieUtils.addCookie(driver, ConfigPropInfo.TEST_DATA_DIR + "cookies/" + ConfigPropInfo.UNION_FRONT_LOGIN_COOKIE_FILE);
        mainPage.openPage();
        if (!mainPage.isLogined()) {
            mainPage.login(userName, password);
            CookieUtils.saveCookie(driver, ConfigPropInfo.TEST_DATA_DIR + "cookies/" + ConfigPropInfo.UNION_FRONT_LOGIN_COOKIE_FILE, ConfigPropInfo.UNION_DOMAIN);
            action.waitForPageLoad();
        }else{
            action.fail("用户退出系统后，服务端会话没有销毁，使用原有cookie仍可登录");
        }
    }
}
